package com.example.megaandroidrssreader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.provider.BaseColumns;
import android.widget.Toast;

public class RssReaderDbHelper extends SQLiteOpenHelper {

	public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "megaandroidrssreader.db";
    
    private static RssReaderDbHelper mInstance;
    
    private RssReaderDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    public static RssReaderDbHelper newInstance(Context context) {
    	if (mInstance == null) {
    		mInstance = new RssReaderDbHelper(context);
    	}
    	return mInstance;
    }

    @Override
	public void onCreate(SQLiteDatabase arg0) {
    	arg0.execSQL(SettingsEntry.SQL_CREATE_TABLE);
    	arg0.execSQL(RssSourcesEntry.SQL_CREATE_TABLE);
    	arg0.execSQL(RssItemsEntry.SQL_CREATE_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(SettingsEntry.SQL_DROP_TABLE);
		db.execSQL(RssSourcesEntry.SQL_DROP_TABLE);
		db.execSQL(RssItemsEntry.SQL_DROP_TABLE);
		onCreate(db);
	}
	
	public static void copyDatabaseToExtSDCardDownloads (Context context) {
		File backupDbDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
		if (backupDbDirectory.canWrite()) {
			String currentDbPath = RssReaderDbHelper.newInstance(context).getReadableDatabase().getPath();
			File currentDb = new File(currentDbPath);
			File backupDb = new File(backupDbDirectory, DATABASE_NAME);
			if (currentDb.exists()) {
				FileInputStream fis = null;
				FileOutputStream fos = null;
				try {
					fis = new FileInputStream(currentDb);
					fos = new FileOutputStream(backupDb);
				} catch (FileNotFoundException e) {
					Toast.makeText(context, "Can't find currend DB file or can't open backup file for writing", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				FileChannel source = fis.getChannel();
				FileChannel destination = fos.getChannel();
				try {
					destination.transferFrom(source, 0, source.size());
				} catch (IOException e) {
					Toast.makeText(context, "Some I/O error happened during DB backup", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				try {
					source.close();
					destination.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			Toast.makeText(context, "Can't write to Download directory", Toast.LENGTH_SHORT).show();
		}
	}

    public static abstract class RssSourcesEntry implements BaseColumns {
    	public static final String TABLE_NAME = "rss_sources";
    	public static final String CN_TITLE = "title";
    	public static final String CN_LINK = "link";
    	public static final String CN_ICON = "icon";
    	
    	private static final String SQL_CREATE_TABLE =
    			"CREATE TABLE " + TABLE_NAME + " (" +
    					_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " +
    					CN_TITLE + " TEXT, " +
    					CN_LINK + " TEXT UNIQUE, " +
    					CN_ICON + " BLOB" +
    					" )";
    					
    	private static final String SQL_DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
    }
    
    public static abstract class RssItemsEntry implements BaseColumns {
    	public static final String TABLE_NAME = "rss_items";
    	public static final String CN_SOURCEID = "sourceId";
    	public static final String CN_TITLE = "title";
    	public static final String CN_LINK = "link";
    	public static final String CN_DESCRIPTION = "description";
    	public static final String CN_FULLTEXT = "fullText";
    	public static final String CN_PUBDATE = "pubDate";
		public static final String CN_PDALINK = "pdalink";
		public static final String CN_DESCRIPTION_UNCUT = "descriptionUncut";
		public static final String CN_FULLTEXT_UNCUT = "fulltextUncut";

    	public static final String CN_IMAGE = "image";

    	private static final String SQL_CREATE_TABLE =
    			"CREATE TABLE " + TABLE_NAME + " (" +
    					_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
    					CN_SOURCEID + " INTEGER, " +
    					CN_TITLE + " TEXT, " +
    					CN_LINK + " TEXT, " +
    					CN_DESCRIPTION + " TEXT, " +
    					CN_FULLTEXT + " TEXT, " +
    					CN_PUBDATE + " TEXT, " +
    					CN_PDALINK + " TEXT, " +
    					CN_DESCRIPTION_UNCUT + " TEXT, " +
    					CN_FULLTEXT_UNCUT + " TEXT, " +
    					CN_IMAGE + " BLOB " +
    					" )";
    					
    	private static final String SQL_DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

    }
 
    public static abstract class SettingsEntry implements BaseColumns {
    	public static final String TABLE_NAME = "settings";
    	public static final String CN_PARAM = "param";
    	public static final String CN_VALUE = "value";

    	private static final String SQL_CREATE_TABLE =
    			"CREATE TABLE " + TABLE_NAME + " (" +
    					_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
    					CN_PARAM + " TEXT UNIQUE, " +
    					CN_VALUE + " TEXT" +
    					" )";
    					
    	private static final String SQL_DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

    }
    
}
